//
//  FeeDeductionView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/11/2.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//  会员卡扣费

#import "FeeDeductionView.h"
#import "MOFSPickerManager.h"

@implementation FeeDeductionView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = BackGroundColor;
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];
        self.scrollView = scrollView;
        
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = CSColorFromRGB(0xfaf6e8);
        [scrollView addSubview:bgView];
        self.bgView = bgView;
        
        UIImageView *headImageView = [[UIImageView alloc] init];
        headImageView.image = [UIImage imageNamed:@"prompt_icon_careful"];
        [bgView addSubview:headImageView];
        self.headImageView = headImageView;
        
        /**
         提示Label
         */
        UILabel *noticeLabel = [[UILabel alloc] init];
        noticeLabel.textColor = CSColorFromRGB(0xb3b3b3);
        noticeLabel.font = SmallFont;
        noticeLabel.text = @"扣费后有效期至：";
        [bgView addSubview:noticeLabel];
        self.noticeLabel = noticeLabel;
        
        /**
         提示
         */
        UILabel *noticeLabel1 = [[UILabel alloc] init];
        noticeLabel1.font = SmallFont;
        noticeLabel1.text = @"2018-09-03";
        noticeLabel1.textColor = CSColorFromRGB(0x0a0a0a);
        [bgView addSubview:noticeLabel1];
        self.noticeLabel1 = noticeLabel1;
        
        /**
         扣费天数、次数
         */
        CommonInputView *feeDeductionInptuView = [[CommonInputView alloc] init];
        feeDeductionInptuView.titleLabel.text = @"扣费(元)*";
        feeDeductionInptuView.titleTF.placeholder = @"请输入金额";
        [scrollView addSubview:feeDeductionInptuView];
        self.feeDeductionInptuView = feeDeductionInptuView;
        
        /**
         退款
         */
        CommonInputView *refundsInputView = [[CommonInputView alloc] init];
        refundsInputView.titleLabel.text = @"退款(元)*";
        refundsInputView.titleTF.placeholder = @"请输入退款金额";
        [scrollView addSubview:refundsInputView];
        self.refundsInputView = refundsInputView;
        
        
        /**
         销售
         */
        CommonBtnView *salBtnView = [[CommonBtnView alloc] init];
        salBtnView.titleLabel.text = @"销售*";
        salBtnView.choseLabel.text = @"请选择";
        salBtnView.choseLabel.textColor = PlaceHoldColor;
        [scrollView addSubview:salBtnView];
        self.salBtnView = salBtnView;
        
        /**
         备注
         */        
        CommonInputView *remarkInputView = [[CommonInputView alloc] init];
        remarkInputView.titleLabel.text = @"备注";
        remarkInputView.titleTF.placeholder = @"请输入备注";
        [scrollView addSubview:remarkInputView];
        self.remarkInputView = remarkInputView;

        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.scrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self);
    }];
    
    /**
     背景
     */
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.width.mas_equalTo(self.scrollView);
        
        make.height.mas_equalTo(40);
    }];
    
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.bgView);
        
        make.left.mas_equalTo(self.bgView).offset(15);
    }];
    
    [self.noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.headImageView);
        
        make.left.mas_equalTo(self.headImageView.mas_right).offset(5);
    }];
    
    [self.noticeLabel1 mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.noticeLabel);
        
        make.left.mas_equalTo(self.noticeLabel.mas_right).offset(2);
    }];
    
    /**
     扣费天数、次数
     */
    [self.feeDeductionInptuView  mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.bgView.mas_bottom);
        
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     退款
     */
    [self.refundsInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.feeDeductionInptuView.mas_bottom);
        
        make.left.width.mas_equalTo(self);
    }];
    
    /**
     设置有效期
     */
    if (self.setValidSwitchView) {
     
        [self.setValidSwitchView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.top.mas_equalTo(self.refundsInputView.mas_bottom).offset(15);
            
            make.left.width.mas_equalTo(self);
        }];
        
        /**
         开始日期
         */
        [self.beginTimeBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.setValidSwitchView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
        /**
         结束日期
         */
        [self.endTimeBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.beginTimeBtnView.mas_bottom);
            make.left.width.mas_equalTo(self);
        }];
        
        /**
         销售
         */
        [self.salBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            if (self.setValidSwitchView.titleSwitch.isOn) {
            
                make.top.mas_equalTo(self.endTimeBtnView.mas_bottom).offset(10);
            }
            else{
                
                make.top.mas_equalTo(self.setValidSwitchView.mas_bottom).offset(10);
            }
            
            make.left.width.mas_equalTo(self);
        }];
    }
    else{
        
        /**
         销售
         */
        [self.salBtnView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.noticeLabel.mas_bottom).offset(10);
            
            make.left.width.mas_equalTo(self);
        }];
    }
    
    
    /**
     备注
     */
    [self.remarkInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.salBtnView.mas_bottom);
        
        make.left.width.mas_equalTo(self);
    }];
 
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    self.scrollView.contentSize = CGSizeMake(self.width, self.remarkInputView.bottom + 10 + adaptNavagationBar);
}

#pragma mark 开关
-(void)switchAction:(UISwitch *)sender{
    
    UISwitch *switchButton = (UISwitch*)sender;
    
    Boolean isOn = [switchButton isOn];
    
    if (isOn) {
        
        /**
         开始日期
         */
        CommonBtnView *beginTimeBtnView = [[CommonBtnView alloc] init];
        beginTimeBtnView.titleLabel.text = @"开始日期";
        beginTimeBtnView.choseLabel.text = @"请选择";
        beginTimeBtnView.choseLabel.textColor = PlaceHoldColor;
        beginTimeBtnView.tag = 0;
        [beginTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        [self addSubview:beginTimeBtnView];
        self.beginTimeBtnView = beginTimeBtnView;
        
        /**
         结束日期
         */
        CommonBtnView *endTimeBtnView = [[CommonBtnView alloc] init];
        endTimeBtnView.titleLabel.text = @"结束日期";
        endTimeBtnView.choseLabel.text = @"请选择";
        endTimeBtnView.choseLabel.textColor = PlaceHoldColor;
        endTimeBtnView.tag = 1;
        [endTimeBtnView.choseBtn addTarget:self action:@selector(beginTimeBtnClick:)];
        [self addSubview:endTimeBtnView];
        self.endTimeBtnView = endTimeBtnView;
        
    }
    else{
        
        [self.beginTimeBtnView removeFromSuperview];
        [self.endTimeBtnView removeFromSuperview];
        self.beginTimeBtnView = nil;
        self.endTimeBtnView = nil;
        
    }
    
    [self setConstraints];
}


#pragma mark beginTimeBtnClick:
-(void)beginTimeBtnClick:(UIButton *)btn{
    
    __weak typeof(self)weakSelf = self;
    
    NSInteger tag = btn.tag;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:1060 title:@"选择日期" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
            
            dateFormatter.dateFormat=@"yyyy-MM-dd";//指定转date得日期格式化形式
            
            if (tag == 0) {
                
                weakSelf.beginTime = [dateFormatter stringFromDate:date];
                weakSelf.beginTimeBtnView.choseLabel.text = weakSelf.beginTime;
                weakSelf.beginTimeBtnView.choseLabel.textColor = TFColor;
            }
            else{
                
                weakSelf.endTime = [dateFormatter stringFromDate:date];
                weakSelf.endTimeBtnView.choseLabel.text = weakSelf.endTime;
                weakSelf.endTimeBtnView.choseLabel.textColor = TFColor;
                
            }
            
        });
        
    } cancelBlock:^{
        
        
    }];
}

-(void)setFeeDedClass:(NSString *)feeDedClass{
    
    _feeDedClass = feeDedClass;
    
    //是次卡
    if ([feeDedClass intValue] == Time) {
        /**
         设置有效期
         */
        CommentSwitchView *setValidSwitchView = [[CommentSwitchView alloc] init];
        setValidSwitchView.titleLabel.text = @"设置有效期";
        [setValidSwitchView.titleSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];   // 开关事件切换通知
        [self addSubview:setValidSwitchView];
        self.setValidSwitchView = setValidSwitchView;
        
        self.feeDeductionInptuView.titleLabel.text = @"扣费(次)*";
        self.feeDeductionInptuView.titleTF.placeholder = @"请输入次数";
        [self setConstraints];
    }
    
    if ([feeDedClass intValue] == Value) {
        
        self.feeDeductionInptuView.titleLabel.text = @"扣费(元)*";
        self.feeDeductionInptuView.titleTF.placeholder = @"请输入金额";
    }
    
    if ([feeDedClass intValue] == Term) {
        
        self.feeDeductionInptuView.titleLabel.text = @"扣费(天)*";
        self.feeDeductionInptuView.titleTF.placeholder = @"请输入天数";
    }
}
@end
